package com.guigusuqi.act.controller;

import com.guigusuqi.act.dto.PublishActivityInfoDTO;
import com.guigusuqi.act.entity.ActPublish;
import com.guigusuqi.act.entity.UserActivity;
import com.guigusuqi.act.service.ActPublishService;
import com.guigusuqi.act.service.UserActivityService;
import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.service.IntegralAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-04-15
 */
@RestController
@Api(tags = {"wx-用户报名管理"})
@RequestMapping("/act/userAct")
public class UserActivityController
{
    @Resource
    private RedisCache redisCache;

    @Resource
    private UserActivityService userActivityService;

    @Resource
    private IntegralAdminService integralAdminService;

    @Resource
    private ActPublishService actPublishService;

    @PostMapping("actRegistration")
    @ApiOperation("用户报名活动")
    public Result actRegistration(@RequestBody PublishActivityInfoDTO publishActivityInfoDTO) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser wxUser =  redisCache.getCacheObject("TOKEN_"+token);
        String openid = wxUser.getUser().getOpenid();

        String actId = publishActivityInfoDTO.getActId();
        publishActivityInfoDTO.setOpenid(openid);

        // 有没有这个活动
        ActPublish act = actPublishService.getById(actId);
        if(Objects.isNull(act))
        {
            return Result.fail().message("没有这个活动!");
        }

        // 查看该用户是否报名
        int count = userActivityService.selectRegisterActivityCount(openid,actId);

        if(count > 0)//该用户已报名
        {
            return Result.fail().message("该用户已报名!");
        }

        // 更新活动的限制人数
        ActPublish actPublish = actPublishService.getById(actId);

        if (actPublish.getLimitNumber() - publishActivityInfoDTO.getPublishNumber() < 0)
        {
            return Result.fail().message("活动报名人数超出限制");
        }

        actPublish.setLimitNumber(actPublish.getLimitNumber()-publishActivityInfoDTO.getPublishNumber());
        actPublishService.updateById(actPublish);

        Result result = userActivityService.actRegistration(publishActivityInfoDTO);

        if(result.getCode() == 200)
        {
            //添加积分
//            Result result1 = integralAdminService.addIntegral("1");
//            if(result1.getCode() == 200)
//            {
//                return Result.success().message("报名成功，并添加对应积分");
//            }else
//            {
//                return Result.success().message("报名成功，添加对应积分失败");
//            }
            return Result.success().message("报名成功");
        }else
        {
            return Result.fail().message("报名失败");
        }
    }

    @GetMapping("selectRegisteredActivity")
    @ApiOperation("查询已报名的活动")
    public Result selectRegisteredActivity() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser wxUser =  redisCache.getCacheObject("TOKEN_"+token);
        String openid = wxUser.getUser().getOpenid();
        return userActivityService.selectRegisteredActivity(openid);
    }

    @GetMapping("deleteRegisteredActivity/{actId}")
    @ApiOperation("取消已报名的活动")
    public Result deleteRegisteredActivity(@PathVariable String actId) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser wxUser =  redisCache.getCacheObject("TOKEN_"+token);
        String openid = wxUser.getUser().getOpenid();
        return userActivityService.deleteRegisteredActivity(openid,actId);
    }

    @GetMapping("selectUserActList")
    @ApiOperation("查看用户活动列表")
    public Result selectUserActList() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser wxUser =  redisCache.getCacheObject("TOKEN_"+token);
        String openid = wxUser.getUser().getOpenid();
        return userActivityService.selectUserActList(openid);
    }

    @GetMapping("selectUserActStatus/{actId}")
    @ApiOperation("查看用户某个活动报名状态")
    public Result selectUserActStatus(@PathVariable String actId) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser wxUser =  redisCache.getCacheObject("TOKEN_"+token);
        String openid = wxUser.getUser().getOpenid();
        return userActivityService.selectUserActStatus(openid,actId);
    }
}

